package com.example.paimengshop;

import javafx.animation.Interpolator;
import javafx.animation.TranslateTransition;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.util.Duration;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class mainController implements Initializable {

    @FXML
    private AnchorPane anchorPane;
    @FXML
    private Button serviceButton;

    @FXML
    private VBox vBox;

    @FXML
    private Button centerButton;

    @FXML
    private Button cartButton;

    @FXML
    private Button sideButton;

    @FXML
    private Button homeButton;

    @FXML
    private ImageView imageView;


    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

        //控件设置
        //侧边栏按钮
        ImageView sideView1=new ImageView();
        sideView1.setImage(new Image("file:PaiMengShop/PaiMengShopClient/src/main/resources/com/example/paimengshop/controllerImage/list.png"));
        sideView1.setFitWidth(40);
        sideView1.setFitHeight(40);

        ImageView sideView2=new ImageView();
        sideView2.setImage(new Image("file:PaiMengShop/PaiMengShopClient/src/main/resources/com/example/paimengshop/controllerImage/cancel.png"));
        sideView2.setFitWidth(40);
        sideView2.setFitHeight(40);

        sideButton.setGraphic(sideView1);//默认为list

        //主页按钮
        ImageView homeView=new ImageView();
        homeView.setImage(new Image("file:PaiMengShop/PaiMengShopClient/src/main/resources/com/example/paimengshop/controllerImage/home.png"));
        homeView.setFitWidth(60);
        homeView.setFitHeight(60);

        homeButton.setGraphic(homeView);

        //购物车按钮
        ImageView cartView=new ImageView();
        cartView.setImage(new Image("file:PaiMengShop/PaiMengShopClient/src/main/resources/com/example/paimengshop/controllerImage/cart.png"));
        cartView.setFitWidth(60);
        cartView.setFitHeight(60);

        cartButton.setGraphic(cartView);

        //在线客服按钮
        ImageView serviceView=new ImageView();
        serviceView.setImage(new Image("file:PaiMengShop/PaiMengShopClient/src/main/resources/com/example/paimengshop/controllerImage/service.png"));
        serviceView.setFitWidth(60);
        serviceView.setFitHeight(60);

        serviceButton.setGraphic(serviceView);

        //个人中心按钮
        ImageView centerView=new ImageView();
        centerView.setImage(new Image("file:PaiMengShop/PaiMengShopClient/src/main/resources/com/example/paimengshop/controllerImage/center.png"));
        centerView.setFitWidth(60);
        centerView.setFitHeight(60);

        centerButton.setGraphic(centerView);

        //设置背景图片
        imageView.setImage(new Image("file:PaiMengShop/PaiMengShopClient/src/main/resources/com/example/paimengshop/beautifyImage/background.png"));


        //设置弹窗动画效果
        sideButton.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
//                System.out.println("clicked");

//                System.out.println(vBox.getLayoutX());

                if(isOpen()) {
                    //open
                    TranslateTransition tt = new TranslateTransition(Duration.seconds(1), vBox);
                    tt.setInterpolator(Interpolator.EASE_OUT);
                    tt.setFromX(0);
                    tt.setToX(100);

                    tt.play();

                    //变成叉叉
                    sideButton.setGraphic(sideView2);
                }else {
                    //close
                    TranslateTransition tt = new TranslateTransition(Duration.seconds(1), vBox);
                    tt.setInterpolator(Interpolator.EASE_OUT);
                    tt.setFromX(100);
                    tt.setToX(0);

                    tt.play();

                    //变成刚刚
                    sideButton.setGraphic(sideView1);
                }

//                System.out.println(vBox.getLayoutX());
            }
        });

        //按钮事件
        homeButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {

                Parent parent = null;
                try {
                    parent = FXMLLoader.load(this.getClass().getResource("home.fxml"));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                anchorPane.getChildren().clear();
                anchorPane.getChildren().add(parent);

            }
        });

        cartButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {

                Parent parent = null;
                try {
                    parent = FXMLLoader.load(this.getClass().getResource("cart.fxml"));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                anchorPane.getChildren().clear();
                anchorPane.getChildren().add(parent);

            }
        });

        serviceButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {

                Parent parent = null;
                try {
                    //parent = FXMLLoader.load(this.getClass().getResource("customerService-View.fxml"));

                    FXMLLoader fxmlLoader = new FXMLLoader(this.getClass().getResource("customerService-View.fxml"));
                    parent = fxmlLoader.load();

                    main.setCustomerServiceController(fxmlLoader.getController());

                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                anchorPane.getChildren().clear();
                anchorPane.getChildren().add(parent);

            }
        });

        centerButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {

                Parent parent = null;
                try {
                    parent = FXMLLoader.load(this.getClass().getResource("center.fxml"));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                anchorPane.getChildren().clear();
                anchorPane.getChildren().add(parent);

            }
        });
    }

    // 判断侧边栏是否打开
    public boolean isOpen(){
        return !(vBox.getTranslateX() == 100);
    }
}
